<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once('src/facebook.php');

class Newhomepage extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->config->load('themes');		
		$theme = $this->config->item('homepage');
		if($theme == "" || !isset($theme))
			$theme = $this->config->item('global_theme');
		
        $this->data['img_dir'] = base_url()."assets/themes/".$theme."/images/";	
        
		$this->load->model('pinoytuner_model');
		$this->load->model('community_model');
		$this->load->helper('text');			
		$pt_bg = $this->data_fetcher->get_home_bg();
        $this->data['pt_bg'] = $pt_bg['strFilename'];
		/*
		-------------------------Load Feeds--------------------------
		Load feeds for each community
		*/
		
		$this->data['dig_feeds'] = $this->pinoytuner_model->fetch_community_feeds('digradio');
		$this->data['planet_feeds'] = $this->pinoytuner_model->fetch_community_feeds('planetjazz');
		$this->data['rice_feeds'] = $this->pinoytuner_model->fetch_community_feeds('riceradio');
		$this->data['yo_feeds'] = $this->pinoytuner_model->fetch_community_feeds('yoradio');

		$countArray = array(count($this->data['dig_feeds']),count($this->data['planet_feeds']),count($this->data['rice_feeds']));
		$this->data['max_feeds'] = max($countArray);
		$this->data['news_feed'] = array();
		for($i = 1, $j = 0; $i < $this->data['max_feeds']; $i++)
		{
			if($j < 4)
			{
				if($j < 4 && array_key_exists($i, $this->data['dig_feeds']))
				{
					array_push($this->data['news_feed'], $this->data['dig_feeds'][$i]);
					$j++;
				}

				if($j < 4 && array_key_exists($i, $this->data['planet_feeds']))
				{
					array_push($this->data['news_feed'], $this->data['planet_feeds'][$i]);
					$j++;
				}

				if($j < 4 && array_key_exists($i, $this->data['rice_feeds']))
				{
					array_push($this->data['news_feed'], $this->data['rice_feeds'][$i]);
					$j++;
				}
			}
			else
			{
				break;
			}
		}
		
		$this->data['video_feed'] = current($this->pinoytuner_model->fetch_videos());
		$this->data['gallery_feed'] = $this->pinoytuner_model->fetch_gallery();
		$this->data['featured_feed'] = $this->pinoytuner_model->fetch_featured_per_community();
		
		/*
		-----------------------END FEEDS--------------------------------
		*/
		/*
		-------------------------Load Themes--------------------------
		Load themes and define theme directory
		*/

		$this->data['css_dir'] = base_url()."assets/themes/".$theme."/css/";
		$this->data['js_dir'] = base_url()."assets/themes/".$theme."/js/";		
		$this->data['img_dir'] = base_url()."assets/themes/".$theme."/images/";		
		$this->theme = $theme;
		
		/*
		-----------------------END THEME--------------------------------
		*/
		
		$this->data['logged_in'] = $this->session->userdata('user_logged');
        $this->data['user'] = $this->session->all_userdata();
		$this->data['ptmenu'] = $this->load->view('common/pt_menu',$this->data,true);
		$this->data['title'] = "Pinoytuner - Believe in Music";
		
		//$this->data['featured_content'] = $this->pinoytuner_model->get_featured_content();
		
		//Remove comment for debugging
		/*
			foreach($this->data['featured_content'] as $c)
			echo $c['strTitle']."<br />";*/
	}
	
	public function index()
	{	
		//Get the Carousel Widget
		//$this->data['carousel'] = $this->bootstrap_carousel($this->data['featured_content']);
		$this->load->view('header',$this->data);		
		$this->load->view('content',$this->data);
		$this->load->view('footer',$this->data);
	}
	
	
	
	//Widgets-----------------------------------------------------------------------------------------------------
	
	function bootstrap_carousel($data)
	{		
		$ctr = 1;
		$ret = $this->load->view('carousel/header',$this->data,true);
		foreach($data as $item)
		{
			if($ctr == 1)
				$ret .= $this->load->view('carousel/item_active',$item,true);
			else
				$ret .= $this->load->view('carousel/item',$item,true);
			$ctr++;
		}
		$ret .= $this->load->view('carousel/footer',$this->data,true);
		
		return $ret;
	}
	 public function post_data()
    {
	
		//wala lang
		$post = $this->input->post();			
        $data['results'] = $this->community_model->fetch_community_data($post['id']);
		$this->data['type'] = $post['type'];

        // Pass the results to the view.
        //$this->load->view('search_results',$data);//
    }
	
    public function logout()
	{		
		$userid = $this->session->userdata('intID');
		$this->data_fetcher->logout_user($userid);
		redirect(base_url()."admin");
	}
    
	public function get_content_info()
	{		
		$post = $this->input->post();				
		
		$this->data['content'] = $this->community_model->fetch_single_content($post['id']);		
		$this->data['type'] = $post['type'];
		
		switch(get_type_name($post['type']))
		{
			case 'gallery':
				$this->data['gallery_images'] = $this->data_fetcher->fetch_table('tb_sys_media',null,null,array('fk-tb_mas_content'=>$post['id']));
				$html = $this->load->view('single_content_gallery_ajax',$this->data,true);		
				break;
			default:
				$html = $this->load->view('single_content_ajax',$this->data,true);		
		}
		
		$data['view_html'] = $html;
		echo json_encode($data);
	}
	
	public function content($type,$id)
	{				
		$this->data['content'] = $this->community_model->fetch_single_content($id);
		$this->data['title'] = $this->data['content']['strTitle'];
		$this->load->view('header_content',$this->data);		
		
		switch($type)
		{
			case 'gallery':
				$this->data['gallery_images'] = $this->data_fetcher->fetch_table('tb_sys_media',null,null,array('fk-tb_mas_content'=>$id));
				$this->load->view('single_content_gallery',$this->data);		
				break;
			default:
				$this->load->view('single_content',$this->data);		
		}
			
		$this->load->view('single_footer',$this->data);
				
	}
    
    public function reserve_slot()
    {
            $post = $this->input->post();
            $post['dteBirthdate'] = date("Y-m-d",strtotime($post['dteBirthdate']));
            $this->db->insert('tb_mas_reservations',$post);
			redirect(base_url().'teeth_reunion');
    }
    public function reserve_slot_validate()
    {
        $post = $this->input->post();
        
        
        $this->load->library('form_validation');

		$this->form_validation->set_rules('strFirstname', 'Firstname', 'required');
		$this->form_validation->set_rules('strLastname', 'Lastname', 'required');
        $this->form_validation->set_rules('strMobileNumber', 'Mobile Number', 'required|numeric|exact_length[11]');
        $this->form_validation->set_rules('dteBirthdate', 'Birthdate', 'required');
		$this->form_validation->set_rules('strEmailAddress', 'Email', 'required|valid_email|is_unique[tb_mas_reservations.strEmailAddress ]');
        $this->form_validation->set_message('is_unique', 'That Email Address is already taken please enter a different Email Address.');

		if ($this->form_validation->run() == FALSE)
		{
            $data['success'] = 0;
            $data['message'] = validation_errors();
            $data['error_firstname'] = form_error('strFirstname');
            $data['error_lastname'] = form_error('strLastname');  
            $data['error_email'] = form_error('strEmailAddress');  
            $data['error_mobile'] = form_error('strMobileNumber');  
            $data['error_bdate'] = form_error('dteBirthdate');  
			echo json_encode($data);
		}
		else
		{
            $data['success'] = 1;
            echo json_encode($data);
            
		}
        
        //$this->db->get_where('tb_mas_reservations');
       
        
    }
	
	/*AJAX GET MORE NEWS FUNCTION*/
	public function getMoreNews()
	{
		$post = $this->input->post(NULL,TRUE);
		$new_feeds = array();
		for($i = $post['offset'], $j = 0; $i < $this->data['max_feeds']; $i++)
		{
			if($j < 4)
			{
				if($j < 4 && array_key_exists($i, $this->data['dig_feeds']))
				{
					array_push($new_feeds, $this->data['dig_feeds'][$i]);
					$j++;
				}

				if($j < 4 && array_key_exists($i, $this->data['planet_feeds']))
				{
					array_push($new_feeds, $this->data['planet_feeds'][$i]);
					$j++;
				}

				if($j < 4 && array_key_exists($i, $this->data['rice_feeds']))
				{
					array_push($new_feeds, $this->data['rice_feeds'][$i]);
					$j++;
				}
			}
			else
			{
				break;
			}
		}
		
		$ret = array();
		$ret['count'] = count($new_feeds);
		foreach($new_feeds as $news)
		{
			if($news['fk-tb_mas_community'] == 1)
			{
				$news['channel_name'] = "DIG RADIO";
                $news['channel_sub'] = "digradio";
			}
			else if($news['fk-tb_mas_community'] == 2)
			{
				$news['channel_name'] = "PLANET JAZZ";
                $news['channel_sub'] = "planetjazz";
			}
			else
			{
				$news['channel_name'] = "RICE RADIO";
                $news['channel_sub'] = "riceradio";
			}

			if($news['fk-tb_mas_content_types'] == 1)
				$news['content_type'] = "News";
			else if($news['fk-tb_mas_content_types'] == 2)
				$news['content_type'] = "Features";
			else if($news['fk-tb_mas_content_types'] == 3)
				$news['content_type'] = "Blogs";
			else if($news['fk-tb_mas_content_types'] == 4)
				$news['content_type'] = "Gallery";
			else if($news['fk-tb_mas_content_types'] == 5)
				$news['content_type'] = "Show";
			else if($news['fk-tb_mas_content_types'] == 14)
				$news['content_type'] = "Reviews";
			else if($news['fk-tb_mas_content_types'] == 15)
				$news['content_type'] = "Event";
			else
				$news['content_type'] = " ";


			$date = date_create($news['dteStart']);
			$news['date'] = date_format($date, 'F j, Y');
			$s = "";
			$s = '<div class="news-feed">
					<div class="news-feed-text left">
						<div class="feed-content">
							<div class="feed-channel-name left '.strtolower(substr($news['channel_name'],0,(strpos($news['channel_name']," ")))).'">
								<p>'.$news['channel_name'].'</p>
							</div>
							<div class="feed-channel-type right">
								<p>'.
									$news['content_type']
								.'</p>
							</div>
							<div class="clear"></div>
							<br/>
							<p class="feed-title">
                                 <a href="'.base_url().$news['channel_sub'].'/content/'.get_type_name($news['fk-tb_mas_content_types']).'/'.$news['intID'].'/'.simplify_title($news['strTitle']).'">
								'.$news['strTitle'].'
                                    </a>
							</p>
							<p class="feed-desc">
								'.$news['strSecondary'].'
							</p>
							<p class="feed-author">
								by '.$news['strAuthor'].'
							</p>
						</div>
						<hr/>
						<div class="feed-stat">
							<p class="left feed-text-date">
								'.$news['date'].'
							</p>
							<p class="right feed-text-vc">
								&nbsp; <img src="'.$this->data['img_dir'].'eye.png"/> &nbsp; <img src="'.$this->data['img_dir'].'speech.png"/>
							</p>
							<div class="clear"></div>
						</div>
					</div>
					<div class="news-feed-img left" style="background: url(\''.IMAGE_UPLOAD_DIR.$news['strPicture'].'\');background-size: cover;background-position: center center;" height="301px" width="261px" >
						
					</div>
					<div class="clear"></div>
				</div>';

			array_push($ret, $s);
		}

		echo json_encode($ret);
	}
	
}